home *** CD-ROM | disk | FTP | other *** search
Text File | 1987-02-13 | 2.1 KB | 94 lines | [TEXT/PJMM] |
- UNIT StringFormat;
-
- { WHAT: Definition of string formatting library}
- { WHO: Joel West, Western Software Technology}
-
- INTERFACE
-
- PROCEDURE SWrite (VAR s : Str255;
- c : CHAR);
- PROCEDURE SWriteHex (VAR s : Str255;
- n : LongInt;
- w : INTEGER);
- PROCEDURE SWriteInt (VAR s : Str255;
- n : LongInt;
- w : INTEGER);
- PROCEDURE SWriteString (VAR s : Str255;
- s2 : Str255);
-
- IMPLEMENTATION
-
- { WHAT: Implementation of UNIT StringWrite}
- { WHO: Joel West, Western Software Technology}
- { WHEN: November 1986}
- { HOW: Formatted output to Pascal strings. Names match}
- { Modula-2 InOut module. Developed to replace —}
- { albeit awkwardly — use of C sprintf.}
-
- { As with all the Pascal equivalents, output the specified field width}
- { or the minimum necessary number of digits, whichever is greater.}
-
- { format a character }
- PROCEDURE SWrite; {(var s : Str255;c : CHAR);}
- VAR
- i : INTEGER;
- BEGIN
- i := length(s) + 1;
- IF i < 255 THEN
- insert(c, s, i);
- END; (* SWrite *)
-
- { format a number in hex }
- PROCEDURE SWriteHex;{(var s : Str255;n : LongInt;w : INTEGER);}
- VAR
- d, i : INTEGER;
- s2 : Str255;
- BEGIN
- s2 := '';
- FOR i := 1 TO w DO
- s2 := concat(s2, ' ');
- WHILE w > 0 DO
- BEGIN
- d := BitAnd(n, $0F);
- n := BitShift(n, -4); {right shift}
-
- IF d < 10 THEN
- IF w < 255 THEN
- BEGIN
- delete(s2, w, 1);
- insert(CHR(ORD('0') + d), s2, w)
- END
- ELSE IF w < 255 THEN
- BEGIN
- delete(s2, w, 1);
- insert(CHR(ORD('A') - 10 + d), s2, w);
- END;
-
- w := w - 1;
- END;
- SWriteString(s, s2);
- END; (* SWriteHex *)
-
- { format a number in decimal }
- PROCEDURE SWriteInt; {(var s : Str255;n : LongInt;w : INTEGER);}
- VAR
- i : INTEGER;
- s2 : Str255;
- BEGIN
- NumToString(n, s2);
- i := w - Length(s2);
- WHILE i > 0 DO
- BEGIN
- SWrite(s, ' '); (* Leading spaces *)
- i := i - 1;
- END;
- SWriteString(s, s2);
- END; (* SWriteInt *)
-
- { format a character string }
- PROCEDURE SWriteString; {(var s : Str255; s2 : Str255);}
- BEGIN
- s := Concat(s, s2);
- END; (* SWriteString *)
-
- END.